﻿using System;
using System.Collections.Generic;

namespace D3.Pathing
{
    public static class Multiverse
    {
        private static int _tickCounter;
        private static bool _drawMap;

        static Multiverse()
        {
            MultiverseMap = new Dictionary<uint, Map>();

            Game.OnTickEvent += Game_OnTickEvent;
            Game.OnSceneActivateEvent += Game_OnSceneActivate;
            Game.OnDrawEvent += Game_OnDrawEvent;
        }

        private static Dictionary<uint, Map> MultiverseMap { get; set; }

        public static void ToogleMapDraw()
        {
            if (_drawMap) _drawMap = false;
            else _drawMap = true;
        }

        private static void Game_OnDrawEvent(EventArgs e)
        {
            if (_drawMap && Game.Ingame) MultiverseMap[Me.WorldId].DrawMap();
        }

        private static void Game_OnSceneActivate(SceneEventArgs e)
        {
            MultiverseMap[Me.WorldId].LoadCollisions(new CachedScene(e.Scene));
        }

        private static void Game_OnTickEvent(EventArgs e)
        {
            if (Game.Ingame && e != null)
            {
                if (_tickCounter++%100 == 0)
                {
                    if (!MultiverseMap.ContainsKey(Me.WorldId))
                        MultiverseMap.Add(Me.WorldId, new Map(World.Get(Me.WorldId)));
                }
            }
        }

        public static bool containsWid()
        {
            return MultiverseMap.ContainsKey(Me.WorldId);
        }

        public static void init()
        {
            //Does Nothing but makes sure me init statics
        }

        public static void Clear()
        {
            MultiverseMap.Clear();
        }

        public static Map GetCurrentMap()
        {
            Map x;

            if (!MultiverseMap.ContainsKey(Me.WorldId))
                x = new Map(World.Get(Me.WorldId));
            else x = MultiverseMap[Me.WorldId];

            return x;
        }
    }
}